#include "excelExporter.h"

ExcelExporter::ExcelExporter()
{

    _paramList = QStringList({"Name",
                              "Equipment",
                              "Operator",
                              "Date",
                              "StartTime",
                              "EndTime",
                              "ExecTime",
                              "Memo",
                              "ExpName",
                              "Method"});
}


int ExcelExporter::exportExcel(QString filePath, QString fileName, QMap<QString, QString> expInfo, QVector<QStringList> expLogs){


    if(!checkOutputPath(filePath))  { return -4; }

    QXlsx::Document xlsx;



    xlsx.addSheet(_sheetName);
    xlsx.selectSheet(_sheetName);

    // 实验信息
    foreach(QString param, _paramList){
        expInfo[param] = expInfo.contains(param) ? expInfo[param] : "";
    }
    expInfo["Equipment"] = "BK-RadPharm-001";
    foreach(QStringList log, expLogs){
        for(int i = 0; i < 4; i ++){
            if(log.size() <= i){
                log.push_back("NULL");
            }
        }
    }

    xlsx.write(1, 1, "名称");
    xlsx.write(1, 3, "设备名称");
    xlsx.write(2, 1, "实验日期");
    xlsx.write(2, 3, "操作人");
    xlsx.write(3, 1, "描述");
    xlsx.write(4, 1, "实验名称");
    xlsx.write(4, 3, "样本名称");
    xlsx.write(5, 1, "方法名称");
    xlsx.write(5, 3, "条码");

    xlsx.write(1, 2, expInfo[_paramList[0]]);
    xlsx.write(1, 4, expInfo[_paramList[1]]);
    xlsx.write(2, 2, expInfo[_paramList[2]]);
    xlsx.write(2, 4, expInfo[_paramList[3]]);
    xlsx.write(3, 2, expInfo[_paramList[4]]);
    xlsx.write(4, 2, expInfo[_paramList[5]]);
    xlsx.write(4, 4, expInfo[_paramList[6]]);
    xlsx.write(5, 2, expInfo[_paramList[7]]);
    xlsx.write(5, 4, expInfo[_paramList[8]]);

    xlsx.write(7, 1, "步骤编号");
    xlsx.write(7, 2, "时间");
    xlsx.write(7, 3, "动作信息");
    xlsx.write(7, 4, "状态");

    int lineIdx = 8;
    foreach(QStringList log,  expLogs){
        xlsx.write(lineIdx, 1, log[0]);
        xlsx.write(lineIdx, 2, log[1]);
        xlsx.write(lineIdx, 3, log[2]);
        xlsx.write(lineIdx, 4, log[3]);
        lineIdx ++;
    }

    if(!xlsx.saveAs(filePath + "/" + fileName))  return -3;

    return 0;
}

bool ExcelExporter::checkOutputPath(QString path){
    QDir dir;
    if(!dir.exists(path)){
        if(!dir.mkpath(path)){
            return false;
        }
    }
    return true;
}


